/**
 *  Editor工具类;
 * @author    KeJun Zheng
 * @copyright (c) 2011,  WayLink
 * @version   0.1
 * @date      <ul>
 * <li>2013-08-07</li>
 * </ul>
 *
 */

var GridEditorUtil = function(){};

/**
 * render as mask chars'
 * @duplicated replaced by getFix({mask:"*",size:6})
 * @param char, size
 * @returns {Object}
 */
GridEditorUtil.getDefault = function(attr) {
	var conf={xtype: 'textfield'};
	Ext.apply(conf,attr);
	return conf;
};

GridEditorUtil.getComposited = function(attr) {
	var conf={ xtype:'compositefield',isComposite:true,isFormField:true,defaults:{xtype: 'textfield'},hideLabel:true,items:[]};
	Ext.apply(conf,attr);
	return conf;
};

GridEditorUtil.getCombox = function(attr) {
	var conf={xtype:'combo',typeAhead:true, triggerAction:'all', mode: 'local', lazyInit:false,lazyRender:true,autoSelect:false,
		store:new  Ext.data.Store({
			data:attr["enums"],
        reader: new Ext.data.JsonReader({
    		fields    : ['sid','rank','code','name','text'],
    		root : 'list', totalProperty : 'size', idProperty:'sid'
		})}), 
		valueField:attr["valueField"] ||"code", displayField:attr["displayField"]||"text",forceSelection:true};
		if( attr.multiSelect ){
			conf.xtype="bizcombo";
			conf.simpleSelect=true;
		}
		Ext.apply(conf,attr);
	return conf;
};

GridEditorUtil.getPassword = function(attr) {
	//TODO
	var conf={ xtype:'compositefield',isComposite:true,isFormField:true,defaults:{xtype: 'textfield'},hideLabel:true,items:[]};
	conf.items.push({name:'dataTO.map.'+attr.name});
	conf.items.push({name:'confirm_'+attr.name, title:'重复输入校验'});
	return conf;
};

/**
 * date render using given format, default is 'Y-m-d H:i:s'
 * @param fmt
 * @param conf
 * @returns Object
 */
GridEditorUtil.getDateTime = function(attr,fmt) {
	var conf={xtype:'datetimefield',format:fmt||'c'};
	conf.altFormats="c|Y-m-d H:i:s|Y-m|H:i:s|H:i|m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j";
	Ext.apply(conf,attr);
	return conf;
};

GridEditorUtil.getNumber = function(attr) {
	var conf={xtype:'numberfield'};
	if(attr.integer){conf.allowDecimals=false; }
	if(attr.positive){conf.allowNegative=false; }
	Ext.apply(conf,attr);
	return conf;
};

GridEditorUtil.getTextArea = function(attr) {
	var conf={xtype:'textarea', height:120};
	Ext.apply(conf,attr);
	return conf;
};

GridEditorUtil.getHTMLEditor = function(attr) {
	var conf={xtype:'htmleditor',width: 400, height: 300};
	Ext.apply(conf,attr);
	return conf;
};


GridEditorUtil.getFixed = function(attr) {
	return GridEditorUtil.getDefault();
};

/**
 * get editor by thr datatype and field attrs
 * @param dtype
 * @param fattr
 * @returns {Object}
 */
GridEditorUtil.get=function(dtype,attr1){
	var	attr = attr1||{};
	var conf = GridEditorUtil.getDefault(attr);
	if( attr.prefix||attr.suffix||attr.unit){
		conf=GridEditorUtil.getFixed(attr);
	}else if(attr.enums){
		conf = GridEditorUtil.getCombox(attr);
	}else switch(dtype){
	/**
	 * java.sql.Types: BIT=-7 TINYINT=-6(8b) BIGINT=-5(64b) NULL=0 CHAR=1 NUMERIC=2 DECIMAL=3 INTEGER=4(32b) SMALLINT=5(16b) FLOAT=6(32b) REAL=7 DOUBLE=8(64b) VARCHAR=12 BOOLEAN=16
	 * DATE =91 TIME=92 TIMESTAMP=93 OTHER=1111 JAVA_OBJECT=2000 ARRAY=2003 BLOB=2004 CLOB=2005 SQLXML=2009
	 */
		case 1:
			if( attr.length>100){
				conf = GridEditorUtil.getTextArea(attr);
			}
			break;
		case 12:
				conf = GridEditorUtil.getTextArea(attr);
			break;
		case 2:
		case 3:
		case 6:
		case 7:
		case 8:
			conf = GridEditorUtil.getNumber(attr);
			break;
		case -7:
		case -6:
		case -5:
		case 0:
		case 4:
		case 5:
			conf = GridEditorUtil.getNumber(attr);
			conf.allowDecimals=false;
			break;
		case 91:
			conf = GridEditorUtil.getDateTime(attr,'Y-m-d');
			break;
		case 92:
			conf = GridEditorUtil.getDateTime(attr,'H:i:s');
			break;
		case 93:
			conf = GridEditorUtil.getDateTime(attr,'Y-m-d H:i:s');
			break;
		case 2005:
		case 2009:
			conf = GridEditorUtil.getHTMLEditor(attr);
			break;
		default:
	}
	return conf;
};
